﻿using System.Diagnostics;
using KinonekoSoftware.Foundation.UI.Commands;
using KinonekoSoftware.Extensions.Collections;

namespace KinonekoSoftware.ArkSpark.Runtimes
{
    partial class ArkSparkMQ
    {
        #region RemoveElement

        

        private void ClearSelection(int index)
        {
            if (_Elements.Count == 1)
            {
                //
                // 清空选择
                ForwardHandler?.Invoke(new NewSelectionEventArgs{EditingItem = null, WYSIWYGEditMode = false});
            }
            else if (index == -1)
            {
                //
                // 清空选择
                ForwardHandler?.Invoke(new NewSelectionEventArgs{EditingItem = null, WYSIWYGEditMode = false});
            }
            else if(index == 0)
            {
                //
                // 选择
                ForwardHandler?.Invoke(new NewSelectionEventArgs{EditingItem = _ElementPresenters[0], WYSIWYGEditMode = false});
            }
            else
            {
                //
                // 选择
                ForwardHandler?.Invoke(new NewSelectionEventArgs{EditingItem = _ElementPresenters[index], WYSIWYGEditMode = false});
            }
        }
        
        private async void DoRemoveElementCommand(DocumentElementPresenter element)
        {
            element ??= Selection;

            if (element is null)
            {
                return;
            }

            if (RemoveElementHandler is not null && !await RemoveElementHandler(element))
            {
                return;
            }

            Remove(element);
        }
        
        
        #endregion


        private void DoNewParagraphCommand()
        {
            Add(CreateParagraph(string.Empty));
        }
        
        
        private void DoNewHeadingText1Command()
        {
            Add(CreateHeadingText(string.Empty, 1));
        }
        
        private void DoNewHeadingText2Command()
        {
            Add(CreateHeadingText(string.Empty, 2));
        }
        
        private void DoNewHeadingText3Command()
        {
            Add(CreateHeadingText(string.Empty, 3));
        }
        
        private void DoNewHeadingText4Command()
        {
            Add(CreateHeadingText(string.Empty, 4));
        }
        
        private void DoNewHeadingText5Command()
        {
            Add(CreateHeadingText(string.Empty, 5));
        }
        
        private void DoNewHeadingText6Command()
        {
            Add(CreateHeadingText(string.Empty, 6));
        }
        
        private void DoNewHeadingText7Command()
        {
            Add(CreateHeadingText(string.Empty, 7));
        }
        
        private void DoNewPictureCommand()
        {
            Add(CreatePicture(string.Empty));
        }

        public Func<DocumentElementPresenter, Task<bool>> RemoveElementHandler { get; set; }

        public ICommandEX NewParagraphCommand    { get; }
        public ICommandEX NewHeadingText1Command { get; }
        public ICommandEX NewHeadingText2Command { get; }
        public ICommandEX NewHeadingText3Command { get; }
        public ICommandEX NewHeadingText4Command { get; }
        public ICommandEX NewHeadingText5Command { get; }
        public ICommandEX NewHeadingText6Command { get; }
        public ICommandEX NewHeadingText7Command { get; }
        public ICommandEX NewPictureCommand      { get; }
        public ICommandEX RemoveElementCommand   { get; }
    }
}